class StudentManagementSystem:
    def __init__(self):
        self.students = []
        self.next_id = 1

    def display_menu(self):
        """显示系统菜单"""
        print("\n" + "=" * 30)
        print("    学生管理系统 V10.0")
        print("=" * 30)
        print("1. 添加学生信息")
        print("2. 删除学生信息")
        print("3. 修改学生信息")
        print("4. 查询所有学生信息")
        print("5. 退出系统")
        print("=" * 30)

    def add_student(self):
        """添加学生信息"""
        print("\n--- 添加学生信息 ---")
        name = input("请输入学生姓名: ").strip()
        gender = input("请输入学生性别: ").strip()
        phone = input("请输入学生手机号: ").strip()

        # 验证输入是否为空
        if not name or not gender or not phone:
            print("错误：所有字段都不能为空！")
            return

        student = {
            'id': self.next_id,
            'name': name,
            'gender': gender,
            'phone': phone
        }

        self.students.append(student)
        self.next_id += 1
        print("添加成功！")

    def delete_student(self):
        """删除学生信息"""
        if not self.students:
            print("学生信息表为空")
            return

        self.show_all_students()

        try:
            student_id = int(input("\n请输入要删除学生的序号: "))

            # 查找学生
            for i, student in enumerate(self.students):
                if student['id'] == student_id:
                    self.students.pop(i)
                    print("删除成功！")
                    return

            print("错误：未找到该序号的学生！")

        except ValueError:
            print("错误：请输入有效的数字序号！")

    def modify_student(self):
        """修改学生信息"""
        if not self.students:
            print("学生信息表为空")
            return

        self.show_all_students()

        try:
            student_id = int(input("\n请输入要修改学生的序号: "))

            # 查找学生
            for student in self.students:
                if student['id'] == student_id:
                    print(
                        f"\n当前信息 - 姓名: {student['name']}, 性别: {student['gender']}, 手机号: {student['phone']}")

                    name = input("请输入修改后的学生姓名: ").strip()
                    gender = input("请输入修改后的学生性别: ").strip()
                    phone = input("请输入修改后的学生手机号: ").strip()

                    # 验证输入是否为空
                    if not name or not gender or not phone:
                        print("错误：所有字段都不能为空！")
                        return

                    # 更新学生信息
                    student['name'] = name
                    student['gender'] = gender
                    student['phone'] = phone

                    print("修改成功！")
                    return

            print("错误：未找到该序号的学生！")

        except ValueError:
            print("错误：请输入有效的数字序号！")

    def show_all_students(self):
        """查询所有学生信息"""
        if not self.students:
            print("学生信息表为空")
            return

        print("\n--- 所有学生信息 ---")
        print("-" * 50)
        print(f"{'序号':<8}{'姓名':<12}{'性别':<8}{'手机号':<15}")
        print("-" * 50)

        for student in self.students:
            print(f"{student['id']:<10}{student['name']:<12}{student['gender']:<8}{student['phone']:<15}")

        print("-" * 50)
        print(f"总计: {len(self.students)} 名学生")

    def run(self):
        """运行学生管理系统"""
        print("欢迎使用学生管理系统！")

        while True:
            self.display_menu()

            try:
                choice = input("请选择功能(1-5): ").strip()

                if choice == '1':
                    self.add_student()
                elif choice == '2':
                    self.delete_student()
                elif choice == '3':
                    self.modify_student()
                elif choice == '4':
                    self.show_all_students()
                elif choice == '5':
                    print("感谢使用学生管理系统，再见！")
                    break
                else:
                    print("错误：请输入1-5之间的数字！")

            except KeyboardInterrupt:
                print("\n\n程序被用户中断，退出系统。")
                break
            except Exception as e:
                print(f"发生错误: {e}")


# 启动系统
if __name__ == "__main__":
    system = StudentManagementSystem()
    system.run()